<?php

class PesanprodukController extends BaseController {

	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/

	public function getPesanproduk()
	{
		$jenis = DB::table('ref_brochure')
					->where('brochure_isdelete','0')
					->get();

		$brosurkertas = DB::table('ref_papertype')
					->where('papertype_type','=','brochure')
					->where('papertype_isdelete','0')
					->get();
		$envelopekertas = DB::table('ref_papertype')
					->where('papertype_type','=','envelope')
					->where('papertype_isdelete','0')
					->get();
		$flayerkertas = DB::table('ref_papertype')
					->where('papertype_type','=','flayer')
					->where('papertype_isdelete','0')
					->get();
		$letterheadkertas = DB::table('ref_papertype')
					->where('papertype_type','=','letterhead')
					->where('papertype_isdelete','0')
					->get();

		$brosursize = DB::table('ref_size_brochure')
					->where('size_brochure_isdelete','0')
					->get();
		$envelopesize = DB::table('ref_size_envelope')
					->where('size_envelope_isdelete','0')
					->get();
		$flayersize = DB::table('ref_size_flayer')
					->where('size_flayer_isdelete','0')
					->get();
		$letterheadsize = DB::table('ref_size_letterhead')
					->where('size_letterhead_isdelete','0')
					->get();

		$jenisidcard = DB::table('ref_idcard_type')
					->where('idcard_type_isdelete','0')
					->get();
		$kualitasidcard = DB::table('ref_idcard_quality')
					->where('idcard_quality_isdelete','0')
					->get();

		$jenisnota = DB::table('ref_bill')
					->where('bill_isdelete','0')
					->groupBy('bill_name')
					->get();
		
		$kertasnota = DB::table('ref_papertype')
					->where('papertype_isdelete','0')
					->where('papertype_type','=','bill')
					->get();
		$jeniskaos = DB::table('ref_tshirt')
					->where('tshirt_isdelete','0')
					->groupBy('tshirt_type_of_material')
					->get();


		$jenispackaging = DB::table('ref_packaging')
					->where('packaging_isdelete','0')
					->groupBy('packaging_type')
					->get();
		$ukuranpackaging = DB::table('ref_packaging')
					->where('packaging_isdelete','0')
					->groupBy('packaging_width')
					->get();
		$kertaspackaging = DB::table('ref_papertype')
					->where('papertype_isdelete','0')
					->where('papertype_type','=','packaging')
					->where('papertype_type','=','packaging')
					->get();
		$jumlahpackaging = DB::table('ref_packaging')
					->where('packaging_isdelete','0')
					->groupBy('packaging_qty_price')
					->get();
		return View::make('admin.neworder.index', compact('kertaspackaging',$kertaspackaging,'jumlahpackaging',$jumlahpackaging,'ukuranpackaging',$ukuranpackaging,'jenispackaging',$jenispackaging,'jeniskaos',$jeniskaos,'kertasnota',$kertasnota,'jenisnota',$jenisnota,'jenisidcard',$jenisidcard,'kualitasidcard',$kualitasidcard,'jenis',$jenis,'brosurkertas',$brosurkertas,'envelopekertas',$envelopekertas,'letterheadkertas',$letterheadkertas,'flayerkertas',$flayerkertas,'brosursize',$brosursize,'envelopesize',$envelopesize,'letterheadsize',$letterheadsize,'flayersize',$flayersize));
	}


	public function get_brosur_price() 
	{
			$brosurcount =DB::table('ref_brochure')
						->where('brochure_type',Input::get('jenis_cetak'))
						->where('brochure_qty_print_page',Input::get('qty_print_page'))
						->where('brochure_papertype_id',Input::get('jenis_kertas'))
						->where('brochure_size_id',Input::get('brosur_size'))
						->where('brochure_color',Input::get('warna'))
						->count();
			$brosur = DB::table('ref_brochure')
						->where('brochure_type',Input::get('jenis_cetak'))
						->where('brochure_qty_print_page',Input::get('qty_print_page'))
						->where('brochure_papertype_id',Input::get('jenis_kertas'))
						->where('brochure_size_id',Input::get('brosur_size'))
						->where('brochure_color',Input::get('warna'))
						->first();
			if($brosurcount == 0){
				$brosur_price = 0;
				$brosur_id = 0;
			}else{
			$brosur_id = $brosur->brochure_id;
			$brosur_price = $brosur->brochure_price;
			}

			if($brosur_id<>0){
				$total_price = $brosur_price * Input::get('jumlah_order');
				Session::put('brosur_id',$brosur_id);
				Session::put('total_price_brosur',$total_price);
				echo json_encode('Rp '.number_format($total_price,0,'.','.'));
			}else{
				$total_price = 0;
				Session::put('brosur_id',$brosur_id);
				Session::put('total_price_brosur',$total_price);
				echo json_encode('Produk custom belum dapat ditentukan harganya');
			}
				
	}

	public function get_idcard_price() 
	{
			$idcardcount =DB::table('ref_idcard_quality')
						->where('idcard_quality_id',Input::get('kualitas_idcard'))
						->count();
			$idcard = DB::table('ref_idcard_quality')
						->where('idcard_quality_id',Input::get('kualitas_idcard'))
						->first();
			if($idcardcount == 0){
				$idcard_price = 0;
				$idcard_id = 0;
			}else{
			$idcard_id = $idcard->idcard_quality_id;
			$idcard_price = $idcard->idcard_quality_price;
			}

			
			
			$arr_fitur_tambahan = json_decode($_POST['arr_fitur_tambahan']);
			$price_add_features = 0;
			if(is_object($arr_fitur_tambahan)) {
				foreach($arr_fitur_tambahan as $fitur_tambahan_id => $fitur_tambahan_value) {
					$id = $fitur_tambahan_value->id; //sesuai attribute name
					$value = $fitur_tambahan_value->value; //1 atau 0
					if($value == 1){
						$ref = DB::table('ref_additional_features')
										->where('additional_features_id',$id)
										->first();
								$price_add_features = $price_add_features+$ref->additional_features_price;
					}
				}
			}

							
			if(Input::get('jumlah_idcard')==""){
				$jumlah = 10;
			}else{
				$jumlah = Input::get('jumlah_idcard');
			}
			
			if($idcard_id<>0){
				$total_price = ($idcard_price + $price_add_features) * $jumlah;
				Session::put('total_price_idcard',$total_price);
				echo json_encode('Rp '.number_format($total_price,0,'.','.'));
			}else{
				$total_price = 0;
				Session::put('total_price_idcard',$total_price);
				echo json_encode('Produk custom belum dapat ditentukan harganya');
			}

	}


		public function get_packaging_price() 
	{		
		if(Input::get('pilihan_ukuran')==1){
			$l = Input::get('length_packaging');
			$w = Input::get('width_packaging');
			$h = Input::get('height_packaging');
		}elseif(Input::get('pilihan_ukuran')==0){
			$size = DB::table('ref_packaging')
					->where('packaging_id',Input::get('ukuran_standar_packaging'))
					->first();
			$l = $size->packaging_length;
			$w = $size->packaging_width;
			$h = $size->packaging_height;
		}

		if(Input::get('pilihan_jumlah')==1){
			$qty = Input::get('jumlah_custom_packaging');
		}else{
			$qty = Input::get('jumlah_standar_packaging');
		}

		
		Input::get('jenis_packaging');
		Input::get('jenis_kertas_packaging');


		$packagingcount = DB::table('ref_packaging')
					->join('ref_papertype','papertype_id','=','packaging_papertype_id')
					->where('packaging_type',Input::get('jenis_packaging'))
					->where('packaging_length',$l)
					->where('packaging_width',$w)
					->where('packaging_height',$h)
					->where('packaging_qty_price',$qty)
					->where('papertype_name',Input::get('jenis_kertas_packaging'))
					->count();
		$packaging = DB::table('ref_packaging')
					->join('ref_papertype','papertype_id','=','packaging_papertype_id')
					->where('packaging_type',Input::get('jenis_packaging'))
					->where('packaging_length',$l)
					->where('packaging_width',$w)
					->where('packaging_height',$h)
					->where('packaging_qty_price',$qty)
					->where('papertype_name',Input::get('jenis_kertas_packaging'))
					->first();
		if($packagingcount == 0){
			$packaging_price = 0;
			$packaging_id = 0;
		}else{
		$packaging_id = $packaging->packaging_id;
		$packaging_price = $packaging->packaging_price;
		}
			
			
			$arr_fitur_tambahan = json_decode($_POST['arr_fitur_tambahan']);
			$price_add_features = 0;
			if(is_object($arr_fitur_tambahan)) {
				foreach($arr_fitur_tambahan as $fitur_tambahan_id => $fitur_tambahan_value) {
					$id = $fitur_tambahan_value->id; //sesuai attribute name
					$value = $fitur_tambahan_value->value; //1 atau 0
					if($value == 1){
						$ref = DB::table('ref_additional_features')
										->where('additional_features_id',$id)
										->first();
								$price_add_features = $price_add_features+$ref->additional_features_price;
					}
				}
			}

			
			
			if($packaging_id<>0){
				$total_price = ($packaging_price + $price_add_features) * $qty;
				Session::put('packaging_id',$packaging_id); 
				Session::put('total_price_packaging',$total_price);
		 		echo json_encode('Rp '.number_format($total_price,0,'.','.'));
			}else{
				$total_price = 0;
				Session::put('packaging_id',$packaging_id); 
				Session::put('total_price_packaging',$total_price);
				echo json_encode('Produk custom belum dapat ditentukan harganya');
			}

	}

		public function get_tshirt_price() 
	{
			if(Input::get('pilihan')=='1'){
				$qty = Input::get('jumlah_custom_kaos');
			}else if(Input::get('pilihan')=='0'){
				$qty = Input::get('jumlah_standar_kaos');
			}
			$tshirtcount = DB::table('ref_tshirt_detail')
					->where('tshirt_detail_tshirt_id',Input::get('jenis_bahan_kaos'))
					->where('tshirt_detail_qty_price',$qty)
					->count();
			$tshirt = DB::table('ref_tshirt_detail')
						->where('tshirt_detail_tshirt_id',Input::get('jenis_bahan_kaos'))
						->where('tshirt_detail_qty_price',$qty)
						->first();
			if($tshirtcount == 0){
				$tshirt_price = 0;
				$tshirtcount = DB::table('ref_tshirt_detail')
					->where('tshirt_detail_tshirt_id',Session::get('jenis_bahan_kaos'))
					->first();
			$tshirt_detail_id = $tshirt->tshirt_detail_id;
			}else{
			$tshirt_id = $tshirt->tshirt_detail_id;
			$tshirt_price = $tshirt->tshirt_detail_price;
			}


			
			$arr_fitur_tambahan = json_decode($_POST['arr_fitur_tambahan']);
			$price_add_features = 0;
			if(is_object($arr_fitur_tambahan)) {
				foreach($arr_fitur_tambahan as $fitur_tambahan_id => $fitur_tambahan_value) {
					$id = $fitur_tambahan_value->id; //sesuai attribute name
					$value = $fitur_tambahan_value->value; //1 atau 0
					if($value == 1){
						$ref = DB::table('ref_additional_features')
										->where('additional_features_id',$id)
										->first();
								$price_add_features = $price_add_features+$ref->additional_features_price;
					}
				}
			}

		
			
			if($tshirt_id<>0){
				$total_price = ($tshirt_price*($qty*12)) + $price_add_features;
				Session::put('total_price_tshirt',$total_price);
				Session::put('tshirt_id',$tshirt_id);
				echo json_encode('Rp '.number_format($total_price,0,'.','.'));
			}else{
				$total_price = 0;
				Session::put('total_price_tshirt',$total_price);
				Session::put('tshirt_id',$tshirt_id);
				echo json_encode('Produk custom belum dapat ditentukan harganya');
			}

	}




	public function get_nota_price() 
	{
		$billcount =DB::table('ref_bill')
					->join('ref_papertype','papertype_id','=','bill_papertype_id')
					->where('bill_id',Input::get('jenis_nota'))
					->where('bill_qty_color',Input::get('jumlah_warna'))
					->where('bill_papertype_id',Input::get('jenis_kertas_nota'))
					->count();
		$bill = DB::table('ref_bill')
					->join('ref_papertype','papertype_id','=','bill_papertype_id')
					->where('bill_id',Input::get('jenis_nota'))
					->where('bill_qty_color',Input::get('jumlah_warna'))
					->where('bill_papertype_id',Input::get('jenis_kertas_nota'))
					->first();
		if($billcount == 0){
			$bill_price = 0;
			$bill_id = Input::get('jenis_nota');

		}else{
		$bill_id = $bill->bill_id;
		$bill_price = $bill->bill_price;
		}


			if($bill_price<>0){
				$total_price = $bill_price * Input::get('order_nota_qty');
				Session::put('total_price_nota',$total_price);
				Session::put('bill_id',$bill_id);
				echo json_encode('Rp '.number_format($total_price,0,'.','.'));
			}else{
				$total_price = 0;
				Session::put('total_price_nota',$total_price);
				Session::put('bill_id',$bill_id);
				echo json_encode('Produk custom belum dapat ditentukan harganya');
			}

	}
	


	//post order
	public function postIdcard()
	{
		if(Input::get('pilihan_jumlah')=="0"){
			$jumlah_idcard = 10;
		}else{
			$jumlah_idcard = Input::get('jumlah_idcard');
		}
		$pilihan_jumlah = Input::get('pilihan_jumlah');
		$jenis_idcard = Input::get('jenis_idcard');
		$kualitas_idcard = Input::get('kualitas_idcard');
		
	
		$customer = New customer();
		$customer->customer_name = Input::get('nama');
		$customer->customer_gender = Input::get('jenis_kelamin');
		$customer->customer_address = Input::get('alamat');
		$customer->customer_phone = Input::get('kontak');
		$customer->customer_email = Input::get('email');
		$customer->save();

		

		$order = New order();
		$order->order_customer_id = $customer->customer_id;
		$order->order_marketing_id = Input::get('marketing_id');
		$order->order_name = 'idcard';
		$order->order_qty = $jumlah_idcard;
		$order->order_qty_unit = 'Card';
		$order->order_total_price = Session::get('total_price_idcard');
		if(Input::hasFile('design_idcard')){
			// ambil file
				$picture = Input::file('design_idcard');
			// simpan file ke directory upload yang terletak di folder public
				$picture->move(public_path().'/design/idcard', $picture->getClientOriginalName());
			// simpan path ke file foto di database
				$order->order_desain = '/design/idcard/'.$picture->getClientOriginalName(); 
		}
		$order->order_date = date('Y-m-d H:i:s');
		$order->save();

		$order = order::Find($order->order_id);
		$order->order_code = $order->order_id.$customer->customer_id.date('md');
		$order->save();

		$typename = refidcardtypes::Find($jenis_idcard);
		$qualityname = refidcardquality::Find($kualitas_idcard);
		$orderidcard = New orderidcard();
		$orderidcard->order_idcard_order_id = $order->order_id;
		$orderidcard->order_idcard_idcard_quality_id = $kualitas_idcard;
		$orderidcard->order_idcard_quality_name = $qualityname->idcard_quality_name;
		$orderidcard->order_idcard_idcard_type_id = $jenis_idcard;
		$orderidcard->order_idcard_type_name = $typename->idcard_type_name;
		$orderidcard->save();

		$ref = DB::table('ref_additional_features')
				->get();
		foreach($ref as $ref){
			if(Input::get($ref->additional_features_id)=='1'){
		
			$orderidcarddetail = New orderidcarddetail();
			$orderidcarddetail->order_idcard_detail_order_idcard_id = $orderidcard->order_idcard_id;
			$orderidcarddetail->order_idcard_detail_idcard_additional_features_id = $ref->additional_features_id;
			$orderidcarddetail->order_idcard_detail_feature_name = $ref->additional_features_name;
			$orderidcarddetail->save();
		
			}
		}

		$invoice = New invoice();
		$invoice->invoice_order_id = $order->order_id;
		$invoice->invoice_total_payment = Session::get('total_price_idcard');
		$invoice->invoice_status = 'Belum lunas';
		$invoice->save();
		
		$progress = New progress;
		$progress->order_progress_order_id = $order->order_id;
		$progress->save();

		$result = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_idcard','order_idcard_order_id','=','order_id')
						->where('order_id','=',$order->order_id)
						->first();
		$ordername = 'Cetak ID Card';

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Order idcard ".$order->order_id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();
		Session::keep(array('username', 'email')); 
return View::make('admin.neworder.orderresult',compact('ordername',$ordername,'result',$result));
	}




	public function postTshirt()
	{
		if(Input::get('pilihan_jumlah')==1){
			$qty = Input::get('jumlah_custom_kaos');
			$jumlah_custom = Input::get('jumlah_custom_kaos');
		}else{
			$qty = Input::get('jumlah_standar_kaos');
			$jumlah_standar = Input::get('jumlah_standar_kaos');
		}
			$jenis_bahan_kaos = Input::get('jenis_bahan_kaos');

		$customer = New customer();
		$customer->customer_name = Input::get('nama');
		$customer->customer_gender = Input::get('jenis_kelamin');
		$customer->customer_address = Input::get('alamat');
		$customer->customer_phone = Input::get('kontak');
		$customer->customer_email = Input::get('email');
		$customer->save();			

		$order = New order();
		$order->order_customer_id = $customer->customer_id;
		$order->order_name = 'tshirt';
		$order->order_marketing_id = Input::get('marketing_id');
		$order->order_qty = $qty;
		$order->order_qty_unit = 'Dozen';
		$order->order_total_price = Session::get('total_price_tshirt');
			if(Input::hasFile('design_kaos')){
				// ambil file
					$picture = Input::file('design_kaos');
				// simpan file ke directory upload yang terletak di folder public
					$picture->move(public_path().'/design/tshirt', $picture->getClientOriginalName());
				// simpan path ke file foto di database
					$order->order_desain = '/design/tshirt/'.$picture->getClientOriginalName(); 
			}
		$order->order_date = date('Y-m-d H:i:s');
		$order->save();

		$order = order::Find($order->order_id);
		$order->order_code = $order->order_id.$customer->customer_id.date('md');
		$order->save();

	
        $tshirtdetail = DB::table('ref_tshirt_detail')
                ->where('tshirt_detail_tshirt_id', $jenis_bahan_kaos)
                ->where('tshirt_detail_qty_price', $qty)
                ->first();

		$tshirt = New ordertshirt;
		$tshirt->order_tshirt_order_id = $order->order_id;
		$tshirt->order_tshirt_tshirt_id = $tshirtdetail->tshirt_detail_id;
		$tshirt->save();

		$ref = DB::table('ref_additional_features')
				->get();
		foreach($ref as $ref){
			if(Input::get($ref->additional_features_id)=='1'){
		
			$ordertshirtdetail = New ordertshirtdetail();
			$ordertshirtdetail->order_tshirt_detail_order_tshirt_id = $tshirt->order_tshirt_id;
			$ordertshirtdetail->order_tshirt_detail_additional_features_id = $ref->additional_features_id;
			$ordertshirtdetail->order_tshirt_detail_additional_features_name = $ref->additional_features_name;
			$ordertshirtdetail->save();
		
			}
		}

		$invoice = New invoice();
		$invoice->invoice_order_id = $order->order_id;
		$invoice->invoice_total_payment = Session::get('total_price_tshirt');
		$invoice->invoice_status = 'Belum lunas';
		$invoice->save();
		
		$progress = New progress;
		$progress->order_progress_order_id = $order->order_id;
		$progress->save();

		$result = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_tshirt','order_tshirt_order_id','=','order_id')
						->where('order_id','=',$order->order_id)
						->first();
		$ordername = 'Cetak T-Shirt';
		

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Order tshirt ".$order->order_id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();

		Session::keep(array('username', 'email')); 
return View::make('admin.neworder.orderresult',compact('ordername',$ordername,'result',$result));
	}



	public function postNota()
	{
		$jenis_nota = Input::get('jenis_nota');
		$jenis_kertas = Input::get('jenis_kertas');
		$jumlah_warna = Input::get('jumlah_warna');
		$qty = Input::get('jumlah_order');

		
		$customer = New customer();
		$customer->customer_name = Input::get('nama');
		$customer->customer_gender = Input::get('jenis_kelamin');
		$customer->customer_address = Input::get('alamat');
		$customer->customer_phone = Input::get('kontak');
		$customer->customer_email = Input::get('email');
		$customer->save();

		$order = New order();
		$order->order_customer_id = $customer->customer_id;
		$order->order_name = 'nota';
		$order->order_marketing_id = Input::get('marketing_id');
		$order->order_qty = $qty;
		$order->order_qty_unit = 'Rim';
		$order->order_total_price = Session::get('total_price_nota');
			if(Input::hasFile('design_nota')){
				// ambil file
					$picture = Input::file('design_nota');
				// simpan file ke directory upload yang terletak di folder public
					$picture->move(public_path().'/design/nota', $picture->getClientOriginalName());
				// simpan path ke file foto di database
					$order->order_desain = '/design/nota/'.$picture->getClientOriginalName(); 

			}
		$order->order_date = date('Y-m-d H:i:s');
		$order->save();

		$order = order::Find($order->order_id);
		$order->order_code = $order->order_id.$customer->customer_id.date('md');
		$order->save();


		$billid = refbill::find(Session::get('bill_id'));


		$bill = New ordernota;
		$bill->order_bill_order_id = $order->order_id;

		$bill->order_bill_bill_id = Session::get('bill_id');
		$bill->order_bill_qty_ply = $billid->bill_qty_ply;
		$bill->order_bill_size = $billid->bill_size;
		$bill->order_bill_name = $billid->bill_name;
		$bill->order_bill_qty_color = $jumlah_warna;		
			$paper = refpapertypes::find($jenis_kertas);
		$bill->order_bill_papertype = $paper->papertype_name;
		$bill->save();

		

		$invoice = New invoice();
		$invoice->invoice_order_id = $order->order_id;
		$invoice->invoice_total_payment = Session::get('total_price_nota');
		$invoice->invoice_status = 'Belum lunas';
		$invoice->save();
		
		$progress = New progress;
		$progress->order_progress_order_id = $order->order_id;
		$progress->save();

		$result = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_bill','order_bill_order_id','=','order_id')
						->where('order_id','=',$order->order_id)
						->first();
		$ordername = 'Cetak Nota';

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Order nota ".$order->order_id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();

		Session::keep(array('username', 'email')); 
return View::make('admin.neworder.orderresult',compact('ordername',$ordername,'result',$result));
	}


	public function postPackaging()
	{
		if(Input::get('pilihan_ukuran')==1){
			Session::put('length',Input::get('length'));
			Session::put('width',Input::get('width'));
			Session::put('height',Input::get('height'));
			Session::put('jenis_ukuran',Input::get('length_packaging').' x '.Input::get('width_packaging').' x '.Input::get('height_packaging'));
		}elseif(Input::get('pilihan_ukuran')==0){
			Session::put('ukuran_standar',Input::get('ukuran_standar_packaging'));
			$size = DB::table('ref_packaging')
					->where('packaging_id',Input::get('ukuran_standar_packaging'))
					->first();
			Session::put('length',$size->packaging_length);
			Session::put('width',$size->packaging_width);
			Session::put('height',$size->packaging_height);
			Session::put('jenis_ukuran',$size->packaging_length.' x '.$size->packaging_width.' x '.$size->packaging_height);

		}

		if(Input::get('pilihan_jumlah_packaging')==1){
			Session::put('qty',Input::get('jumlah_custom_packaging'));
			Session::put('jumlah_custom',Input::get('jumlah_custom_packaging'));
		}else{
			Session::put('qty',Input::get('jumlah_standar_packaging'));
			Session::put('jumlah_standar',Input::get('jumlah_standar_packaging'));
		}

		$jenis_packaging = Input::get('jenis_packaging');
		$jenis_kertas = Input::get('jenis_kertas_packaging');
		$pilihan_jumlah = Input::get('pilihan_jumlah_packaging');
		$pilihan_ukuran = Input::get('pilihan_ukuran');

		


		$customer = New customer();
		$customer->customer_name = Input::get('nama');
		$customer->customer_gender = Input::get('jenis_kelamin');
		$customer->customer_address = Input::get('alamat');
		$customer->customer_phone = Input::get('kontak');
		$customer->customer_email = Input::get('email');
		$customer->save();
		
		$order = New order();
		$order->order_customer_id = $customer->customer_id;
		$order->order_name = 'packaging';
		$order->order_marketing_id = Input::get('marketing_id');
		$order->order_qty = Session::get('qty');
		$order->order_qty_unit ='Pcs';
		$order->order_total_price = Session::get('total_price_packaging');
			if(Input::hasFile('design_packaging')){
				// ambil file
					$picture = Input::file('design_packaging');
				// simpan file ke directory upload yang terletak di folder public
					$picture->move(public_path().'/design/packaging', $picture->getClientOriginalName());
				// simpan path ke file foto di database
				$order->order_desain = '/design/packaging/'.$picture->getClientOriginalName(); 
			}
		$order->order_date = date('Y-m-d H:i:s');
		$order->save();

		$order = order::Find($order->order_id);
		$order->order_code = $order->order_id.$customer->customer_id.date('md');
		$order->save();

		$packaging = New orderpackaging;
		$packaging->order_packaging_order_id = $order->order_id;
		$packaging->order_packaging_packaging_id = Session::get('packaging_id');
		$packaging->order_packaging_type =$jenis_packaging;
		$packaging->order_packaging_size = Session::get('jenis_ukuran');
		$packaging->order_packaging_paper = $jenis_kertas;
		$packaging->save();

		$ref = DB::table('ref_additional_features')
				->get();
		foreach($ref as $ref){
			if(Input::get($ref->additional_features_id)=='1'){
		
			$orderpackagingdetail = New orderpackagingdetail();
			$orderpackagingdetail->order_packaging_detail_order_packaging_id = $packaging->order_packaging_id;
			$orderpackagingdetail->order_packaging_detail_additional_features_id = $ref->additional_features_id;
			$orderpackagingdetail->order_packaging_detail_additional_features_name = $ref->additional_features_name;
			$orderpackagingdetail->save();
		
			}
		}

		$invoice = New invoice();
		$invoice->invoice_order_id = $order->order_id;
		$invoice->invoice_total_payment = Session::get('total_price_packaging');
		$invoice->invoice_status = 'Belum lunas';
		$invoice->save();
		
		$progress = New progress;
		$progress->order_progress_order_id = $order->order_id;
		$progress->save();

		$result = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_packaging','order_packaging_order_id','=','order_id')
						->where('order_id','=',$order->order_id)
						->first();
		$ordername = 'Cetak Packaging';

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Order packaging ".$order->order_id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();


		Session::keep(array('username', 'email')); 
return View::make('admin.neworder.orderresult',compact('ordername',$ordername,'result',$result));

	}


	public function postBrosur()
	{


		$jenis_cetak = Input::get('jenis_cetak');
		$jenis_kertas = Input::get('jenis_kertas');
		$brosur_size = Input::get('brosur_size');
		$warna = Input::get('warna');
		$qty_print_page = Input::get('qty_print_page');
		$qty = Input::get('order_brosur_qty');
		$brosur_unit = Input::get('order_brosur_qty_unit');

	

		$customer = New customer();
		$customer->customer_name = Input::get('nama');
		$customer->customer_gender = Input::get('jenis_kelamin');
		$customer->customer_address = Input::get('alamat');
		$customer->customer_phone = Input::get('kontak');
		$customer->customer_email = Input::get('email');
		$customer->save();

		$order = New order();
		$order->order_customer_id = $customer->customer_id;
		$order->order_name = 'brochure';
		$order->order_marketing_id = Input::get('marketing_id');
		$order->order_qty = $qty;
		$order->order_qty_unit = $brosur_unit;
		$order->order_total_price = Session::get('total_price_brosur');
		if(Input::hasFile('design_brosur')){
			// ambil file
				$picture = Input::file('design_brosur');
			// simpan file ke directory upload yang terletak di folder public
				$picture->move(public_path().'/design/brosur', $picture->getClientOriginalName());
			// simpan path ke file foto di database
				$order->order_desain = '/design/brosur/'.$picture->getClientOriginalName(); 
		}
		$order->order_date = date('Y-m-d H:i:s');
		$order->save();

		$order = order::Find($order->order_id);
		$order->order_code = $order->order_id.$customer->customer_id.date('md');
		$order->save();



		if(Session::get('brosur_id')<>0){
							$brosurid = refbrosur::find(Session::get('brosur_id'));

							if($brosurid->brochure_type == "brochure"){
								$size = refbrochuresize::find(Input::get('brosur_size'));
								$sizename = $size->size_brochure_name;
							}else if($brosurid->brochure_type == "envelope"){
								$size = refenvelopesize::find(Input::get('brosur_size'));
								$sizename = $size->size_envelope_length.' x '.$size->size_envelope_width.' '. $size->size_envelope_unit;
							}else if($brosurid->brochure_type == "flayer"){
								$size = refflayersize::find(Input::get('brosur_size'));
								$sizename = $size->size_flayer_length.' x '.$size->size_flayer_width.' '. $size->size_flayer_unit;
							}else if($brosurid->brochure_type == "letterhead"){
								$size = refletterheadsize::find(Input::get('brosur_size'));
								$sizename = $size->size_letterhead_length.' '. $size->size_letterhead_width.' '. $size->size_letterhead_unit;
							}
		}elseif(Session::get('brosur_id')==0){
							if(Input::get('jenis_cetak')=="brochure"){
								$size = refbrochuresize::find(Input::get('brosur_size'));
								$sizename = $size->size_brochure_name;
							}else if(Input::get('jenis_cetak')=="envelope"){
								$size = refenvelopesize::find(Input::get('brosur_size'));
								$sizename = $size->size_envelope_length.' x '.$size->size_envelope_width.' '. $size->size_envelope_unit;
							}else if(Input::get('jenis_cetak')=="flayer"){
								$size = refflayersize::find(Input::get('brosur_size'));
								$sizename = $size->size_flayer_length.' x '.$size->size_flayer_width.' '. $size->size_flayer_unit;
							}else if(Input::get('jenis_cetak')=="letterhead"){
								$size = refletterheadsize::find(Input::get('brosur_size'));
								$sizename = $size->size_letterhead_length.' '. $size->size_letterhead_width.' '. $size->size_letterhead_unit;
							}
		}


		$brochure = New orderbrochure;
		$brochure->order_brochure_order_id = $order->order_id;

		$brochure->order_brochure_brochure_id = Session::get('brosur_id');
		$brochure->order_brochure_size = $sizename;
		
		$brochure->order_brochure_type = $jenis_cetak;
		$brochure->order_brochure_color = $warna;		
		$brochure->order_brochure_qty_print_page = $qty_print_page;	
			$paper = refpapertypes::find($jenis_kertas);
			$brochure->order_brochure_papertype = $paper->papertype_name;
		$brochure->save();	



		$invoice = New invoice();
		$invoice->invoice_order_id = $order->order_id;
		$invoice->invoice_total_payment = Session::get('total_price_brosur');
		$invoice->invoice_status = 'Belum lunas';
		$invoice->save();
		
		$progress = New progress;
		$progress->order_progress_order_id = $order->order_id;
		$progress->save();

		$result = DB::table('sys_order')
						->join('sys_customer','customer_id','=','order_customer_id')
						->join('sys_order_brochure','order_brochure_order_id','=','order_id')
						->where('order_id','=',$order->order_id)
						->first();
		$ordername = 'Cetak Brosur';

		$log = New adminlog();
		$log->admin_log_admin_id = Session::get('admin_id');	
		$log->admin_log_activity = "Order brosur ".$order->order_id;	
		$log->admin_log_date = date('Y-m-d H:i:s');	
		$log->save();


		Session::keep(array('username', 'email')); 
return View::make('admin.neworder.orderresult',compact('ordername',$ordername,'result',$result));
	}

}